<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

class hotmovie {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['action'];			
		$params = $this->url['params']['1'];
		if($params == 'hotchart'){
			$this->hotChart();
		}elseif($params == 'popularchart'){
			$this->popularChart();
		}else{
			$this->defaultAction();
		}
		exit();
	}

	function defaultAction(){
		exit('ssd');
	}

	function hotChart(){
		//评论走势 根据4部电影近2周短评+影评的数量，绘制出走势图；横轴是时间、纵轴是“评论”个数量
		//喜欢走势 根据用户的短评、影评的打分在5.5以上评价个数，绘制出走势图；横轴是时间、纵轴是喜欢个数量
		require_once(APP_PATH."model/rating.php" );
		$db  = getDB();
		$data = new RatingModel($db);		
		function getDateList(){
			$date = date("Y-m-d",mktime(0,0,0,date("m"),date("d")+1-14,date("Y")));
			$start = strtotime($date);
			$end = strtotime(date("Y-m-d"));			
			$ret = array();
			$i = 0;
			while($start <= $end){
				$res[] = date('m-d',$start);
				$tmp = strtotime("+1 days",$start);
				if($end <= $tmp){
					$res[] = date('m-d',$end);
				}else{
					$res[] = date('m-d',$tmp);
				}
				$i++;
				$start = strtotime("+1 day",$tmp);
			}
			return $res;
		}
		function getArchive($archiveList){	
			$archive = '';
			$resultdates = array();
			foreach ($archiveList as $timearray){ 
				$m = gmdate('m', $timearray['addtime']); 
				$d = gmdate('d', $timearray['addtime']);
				$resultdates[$m][$d] += 1;   
			}
			$i = 0;
			foreach ($resultdates as $k => $v){				
				foreach ($v as $k1 => $v1){
					$times =  $k.'-'.$k1;
					$archive[$times]['count'] = $v1;
					$i++;
				}
			}
			return $archive;
		}		
		$cache = DATA_PATH."cache/hotmovies.php";
		if(is_file($cache)){
			$hotmovies = require_once($cache);
		}
		foreach($hotmovies as $k => $v){
			$movieids .= "'".$v['id']."',";
		}
		$movieids = substr($movieids,0,-1);

		
		$comments = $data->getTwoWeekCommentsByIds($movieids);
		$reviews = $data->getTwoWeekReviewsByIds($movieids);
		//de($reviews);
		$i = 0;
		foreach($comments as $k => $v ){
			$new_coments[$v['mid']][$i]['mid'] = $v['mid'];
			$new_coments[$v['mid']][$i]['type'] = $v['type'];
			$new_coments[$v['mid']][$i]['rating'] = $v['rating'];
			$new_coments[$v['mid']][$i]['addtime'] = $v['addtime'];
			$i++;			
		}		
		
		$i = 0;
		foreach($reviews as $k => $v ){
			$new_reviews[$v['mid']][$i]['mid'] = $v['mid'];
			$new_reviews[$v['mid']][$i]['rating'] = $v['rating'];
			$new_reviews[$v['mid']][$i]['addtime'] = $v['addtime'];
			$i++;			
		}
		//de($new_coments);
		//de($new_reviews);				

		foreach($new_coments as $k => $v ){
			$cList[$k]  = getArchive($v);			
			if($cList){
				$cMax[$k] = max($cList[$k]);
			}			
		}
		foreach($new_reviews as $k => $v ){
			$rList[$k]  = getArchive($v);			
			if($rList){
				$rMax[$k] = max($rList[$k]);
			}			
		}
		//de($cList);
		//de($rList);

		$dateList = getDateList();
		
		foreach($hotmovies as $ks => $vs ){			
			$mid = $vs['id'];
			foreach($dateList as $k => $v ){
				$theCV = $cList[$mid][$v];
				$theRV = $rList[$mid][$v];
				if(!empty($theCV)){
					$cNum[$mid][$v] = $theCV['count'];
				}else{
					$cNum[$mid][$v] = '0';
				}
				if(!empty($theRV)){
					$rNum[$mid][$v] = $theRV['count'];
				}else{
					$rNum[$mid][$v] = '0';
				}
			}
			$i = $ks;

			$s = array_merge_recursive($rNum[$mid],$cNum[$mid]);
			foreach($s as $kss => $vss ){
				$newNum[$mid] .= array_sum($s[$kss]).',';
			}
			
			$newNums[$i]['data'] = substr($newNum[$mid],0,-1);
			$newNums[$i]['name'] = $vs['title'];			
		}

		foreach($dateList as $k => $v ){
			$date .= '"'.$v.'",';
		}

		$date = substr($date,0,-1);
		
		$max = $cMax['count'] >= $rMax['count'] ?  $cMax['count'] : $rMax['count'];	

		echo '

{
	"elements":[
		{
			"type":"line",
			"fill-alpha": 0.4,
			"alpha":     0.5,
			"tool_tip" : "Visitors: #val#",
			"dot-style": {"type":"solid-dot", "dot-size": 2, "halo": 1},
			"values":['.$newNums[0]['data'].'],
			"colour":"#ff6600",
			"text":"'.$newNums[0]['name'].'",
			 "width": 2,
			"font-size":10
		},
		{
			"type":"line",
			"values":['.$newNums[1]['data'].'],
			"colour":"#736AFF",
			"text":"'.$newNums[1]['name'].'",
			"font-size":11
		},
		{
			"type":"line",
			"dot-style": {"type":"solid-dot", "dot-size": 2, "halo": 1},
			"values":['.$newNums[2]['data'].'],
			"colour":"#993333",
			"text":"'.$newNums[2]['name'].'",
			"font-size":11
		},
		{
			"type":"line",
			"values":['.$newNums[3]['data'].'],
			"colour":"#8d4bbb",
			"text":"'.$newNums[3]['name'].'",
			"font-size":11
		}
	],
	"y_axis":{
		"stroke":1,
		"colour":"#c6d9fd",
		"grid-colour":"#ffffff"
	},
	"x_axis":{
		"offset":false,
		"stroke":1,
		"colour":"#c6d9fd",
		"grid-colour":"#ffffff",
		"labels":{
			"labels":['.$date.'],
			"visible": true,
			"rotate":45,
			"steps":2
			}
	},
	"bg_colour":"#ffffff"
}
';
	}
}

?>
